package org.abettor.angleworm.ui;

import java.io.IOException;

import org.abettor.angleworm.auth.AccountType;
import org.abettor.angleworm.i18n.Translator;
import org.abettor.angleworm.util.Configuration;
import org.abettor.util.Environment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Button;

public class AccountTypeDialog
{
	private static Translator translator = null;
	private static final int WIDTH = 300;
	private static final int HEIGHT = 240;
	private AccountType accountType = null;
	private Display display = Display.getDefault();
	private Shell mainShell = null;
	private Label labelBanner = null;
	private Group group = null;
	private Button buttonGoogle = null;
	private Button buttonYahoo = null;
	private Button buttonMsn = null;
	private Button buttonExit = null;
	private Button checkRemember = null;

	/**
	 * 显示登录窗口
	 */
	public void show()
	{
		createMainShell();
		mainShell.open();
		while(! mainShell.isDisposed())
		{
			if(! display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	private void createMainShell()
	{
		try
		{
			translator = Translator.getInstance();
			if(translator == null)
			{
				translator = Translator.initialize("en_US");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 0;
		gridLayout.makeColumnsEqualWidth = true;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		GridData gridDataBanner = new GridData();
		gridDataBanner.horizontalAlignment = GridData.FILL;
		gridDataBanner.grabExcessHorizontalSpace = true;
		gridDataBanner.verticalAlignment = GridData.BEGINNING;
		gridDataBanner.heightHint = 60;
		Monitor monitor = display.getPrimaryMonitor();
		Rectangle rect = monitor.getBounds();
		mainShell = new Shell(SWT.MIN | SWT.CLOSE);
		mainShell.setText(translator.getString("accountType.dialog.title"));
		mainShell.setImage(new Image(display, Environment.getFilePath("img/logo.png")));
		mainShell.setLayout(gridLayout);
		mainShell.setSize(new Point(WIDTH, HEIGHT));
		mainShell.setLocation((rect.width - WIDTH) / 2, (rect.height - HEIGHT) / 2);
		labelBanner = new Label(mainShell, SWT.SHADOW_NONE);
		labelBanner.setLayoutData(gridDataBanner);
		labelBanner.setImage(new Image(display, Environment.getFilePath("img/banner.png")));

		createGroup();

		checkRemember = new Button(mainShell, SWT.CHECK);
		checkRemember.setText(translator.getString("accountType.dialog.button.remember"));
		checkRemember.setLayoutData(new GridData());
		AccountType accType = Configuration.getDefaultAccountType();
		if(accType != null)
		{
			checkRemember.setSelection(true);
		}

		GridData layoutDataExit = new GridData();
		layoutDataExit.grabExcessHorizontalSpace = true;
		layoutDataExit.horizontalAlignment = GridData.END;
		buttonExit = new Button(mainShell, SWT.NONE);
		buttonExit.setText(translator.getString("accountType.dialog.button.exit"));
		buttonExit.setLayoutData(layoutDataExit);

		buttonExit.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				mainShell.close();
			}
		});
		SelectionAdapter choiceListener = new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				accountType = (AccountType) e.widget.getData();
				boolean rememberType = checkRemember.getSelection();
				if(rememberType)
				{
					Configuration.setDefaultAccountType(accountType);
					Configuration.save();
				}
				else
				{
					Configuration.setDefaultAccountType(null);
					Configuration.save();
				}
				mainShell.close();
			}
		};
		buttonGoogle.addSelectionListener(choiceListener);
		buttonYahoo.addSelectionListener(choiceListener);
		buttonMsn.addSelectionListener(choiceListener);
	}

	private void createGroup()
	{
		RowLayout rowLayoutGrp = new RowLayout();
		rowLayoutGrp.wrap = true;
		rowLayoutGrp.type = SWT.HORIZONTAL;
		GridData gridDataGrp = new GridData();
		gridDataGrp.grabExcessHorizontalSpace = true;
		gridDataGrp.horizontalAlignment = GridData.FILL;
		group = new Group(mainShell, SWT.NONE);
		group.setLayoutData(gridDataGrp);
		group.setLayout(rowLayoutGrp);
		group.setText(translator.getString("accountType.dialog.group"));

		buttonGoogle = new Button(group, SWT.NONE);
		buttonGoogle.setData(AccountType.google);
		buttonGoogle.setText(translator.getString("accountType.dialog.button.google"));
		buttonGoogle.setImage(new Image(display, Environment.getFilePath("img/brand/google.png")));

		buttonYahoo = new Button(group, SWT.NONE);
		buttonYahoo.setData(AccountType.yahoo);
		buttonYahoo.setText(translator.getString("accountType.dialog.button.yahoo"));
		buttonYahoo.setImage(new Image(display, Environment.getFilePath("img/brand/yahoo.png")));

		buttonMsn = new Button(group, SWT.NONE);
		buttonMsn.setData(AccountType.msn);
		buttonMsn.setText(translator.getString("accountType.dialog.button.msn"));
		buttonMsn.setImage(new Image(display, Environment.getFilePath("img/brand/msn.png")));
	}

	public AccountType getAccountType()
	{
		return accountType;
	}
}
